/**
 * @author SilverBiology, LLC
 * @website http://code.google.com/p/gbif-spreadsheet-processor/
*/
Ext.namespace('GBIF');
Ext.namespace('GBIF.GeoSpatial')

GBIF.GeoSpatial.FeatureDataGrid = function(config){

	var store = new Ext.data.GroupingStore({
		reader: new Ext.data.JsonReader({
			fields: [
					{name:'name', type:'string',mapping:'properties.name'}
				,	{name:'id', type:'string'}
				,	{name:'state', type:'string'}
				,	{name:'attribute', type:'string',mapping:'properties.attribute'}
			]
		})
	})
	
	Ext.apply(this,config,{
			border: false
		,	split: true
		,	layer: ''
		,	store: store
		,	id: 'featuredatagrid'
		,	autoScroll: true
		,	loadMask: true
		,	markDirty: false
		,	view: new Ext.grid.GroupingView({
				markDirty: false
			})
		,	sm: new Ext.grid.RowSelectionModel({singleSelect:true})
		,	columns: [{
				header: "id", dataIndex:'id',width:200
			},{
				header: "state", dataIndex:'state'
			},{			
					header: "Name"
				, dataIndex:'name'
				,	editor: new Ext.form.TextField()
				,	markDirty: false
			},{
					header: "Attribute"
				,	dataIndex:'attribute'
				,	editor: new Ext.form.TextField()
				,	markDirty: false
			}]
		,	tbar:[{
					text: 'Commit'
				,	ref: '../commit'
			}]
		, listeners: {
				rowcontextmenu: this.rightClickMenu
			} 			
	});

	GBIF.GeoSpatial.FeatureDataGrid.superclass.constructor.call(this, config);

}

Ext.extend(GBIF.GeoSpatial.FeatureDataGrid, Ext.grid.EditorGridPanel, {

		rightClickMenu: function(grid, row, e){
			grid.getSelectionModel().selectRow(row);
			this.fireEvent('rowClick',grid,row,this);
			var record = grid.getSelectionModel().getSelected().data;
			var items = [];
			items.push({
					text: 'Delete Feature'
				, scope: this
				, handler: function() {
						Ext.MessageBox.confirm("Confirm","Are you Sure?", this.deleteNode);
					}
			}, {
					text: 'Center On Map'
				, scope: this
				, handler: function() {
						var rec = grid.store.getAt(row);
						var lat = rec.json.geometry.coordinates[0].lat;
						var lon = rec.json.geometry.coordinates[0].lon;
						this.fireEvent("centerToMap",lon,lat,this);
					}
			});
			
			var menu = new Ext.menu.Menu({
				items: items
			});
			var xy = e.getXY();
			menu.showAt(xy);
		}

	,	deleteNode:function(btn, text){
			if (btn == 'yes') {
				var grid = Ext.getCmp('featuredatagrid');
				grid.fireEvent('deleteFeature',this);
			}
		}

});